package hczc.longcai.com.hccrowdfunding.utils.json;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import hczc.longcai.com.hccrowdfunding.activity.beans.Code;
import hczc.longcai.com.hccrowdfunding.activity.beans.EditPass;
import hczc.longcai.com.hccrowdfunding.activity.beans.FindPass;
import hczc.longcai.com.hccrowdfunding.activity.beans.LoginInfo;
import hczc.longcai.com.hccrowdfunding.activity.beans.RegInfo;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.beans.UserInfo;
import hczc.longcai.com.hccrowdfunding.database.DBIndexController;
import hczc.longcai.com.hccrowdfunding.home.first_index.beans.Banner;
import hczc.longcai.com.hccrowdfunding.home.first_index.beans.Information;
import hczc.longcai.com.hccrowdfunding.home.first_index.beans.ProjectInfo;

/**
 * 解析JSON类
 * Created by Administrator on 2016/2/24.
 */
public class ParseJSON {

    private static final String TAG = "ParseJSON";

    /**
     * 成功的状态码
     */
    public static final String STATE_SUCCESS = "1";

    /**
     * 失败的状态码
     */
    public static final String STATE_FAILURE = "0";

    /**
     * 解析获取到的用户信息并存入数据库
     * @param JSON
     * @throws JSONException
     */
    public static boolean parseUser(String JSON) throws JSONException {
        UserInfo info = null;
        boolean flag = false;
        String c_JSON;
        if (JSON != null) {
            c_JSON = JSON;
            JSONObject jsonObject = new JSONObject(c_JSON);
            String status = jsonObject.getString("status");
            if (STATE_SUCCESS.equals(status)){
                jsonObject = jsonObject.getJSONObject("minfo");

                String id = jsonObject.getString("id");
                String user_name = jsonObject.getString("user_name");
                String user_email = jsonObject.getString("user_email");
                String user_phone = jsonObject.getString("user_phone");
                String sex = jsonObject.getString("sex");
                String real_name = jsonObject.getString("real_name");
                String idcard = jsonObject.getString("idcard");
                String money_freeze = jsonObject.getString("money_freeze");
                String money_collect = jsonObject.getString("money_collect");
                String account_money = jsonObject.getString("account_money");
                String userpic = jsonObject.getString("userpic");
                info = new UserInfo(id,user_name,user_email,user_phone,sex,real_name,idcard,money_freeze,money_collect,account_money,userpic);
                BaseApplication.dbUserInfoController.saveInfo(info);
                flag = true;
            }else {
                String tips = jsonObject.getString("tips");
                Log.d(TAG,tips);
                flag = false;
            }
        }

        return flag;

    }


    /**
     * 解析修改密码返回的JSON
     * @param JSON 修改密码返回的JSON
     * @return 解析后的数据存储在实体类中
     * @throws JSONException
     */
    public static EditPass parseEdit(String JSON) throws JSONException {
        EditPass editPass = null;
        String c_JSON;
        if (JSON != null) {
            c_JSON = JSON;
            JSONObject jsonObject = new JSONObject(c_JSON);
            String status = jsonObject.getString("status");
            if (STATE_FAILURE.equals(status)){
                String tips = jsonObject.getString("tips");
                editPass = new EditPass(status,tips);
            }else {
                editPass = new EditPass(status);
            }
        }
        return editPass;
    }

    /**
     * 解析找回密码返回的JSON
     * @param JSON 找回密码返回的JSON
     * @return 解析后存储的实体类
     * @throws JSONException
     */
    public static FindPass parseFind(String JSON) throws JSONException {
        FindPass findPass = null;
        String c_JSON;
        if (JSON != null) {
            c_JSON = JSON;
            JSONObject jsonObject = new JSONObject(c_JSON);
            String status = jsonObject.getString("status");
            if (STATE_FAILURE.equals(status)){
                String tips = jsonObject.getString("tips");
                findPass = new FindPass(status,tips);
            }else {
                findPass = new FindPass(status);
            }

        }
        return findPass;
    }

    /**
     * 解析登陆时返回的JSON
     * @param JSON 需要解析的JSON字符串
     * @return 解析后存储到一个类中
     * @throws JSONException
     */

    public static LoginInfo parseLogin(String JSON) throws JSONException {
        LoginInfo info = null;
        String c_JSON;
        if (JSON != null) {
            c_JSON = JSON;
            JSONObject jsonObject = new JSONObject(c_JSON);
            String status = jsonObject.getString("status");
            String tips = jsonObject.getString("tips");
            if (STATE_SUCCESS.equals(status)){
                String uid = jsonObject.getString("uid");
                String user_name = jsonObject.getString("user_name");
                info = new LoginInfo(status,uid,user_name,tips);
            }else {
                info = new LoginInfo(status,tips);
            }
        }

        return info;
    }

    /**
     * 解析验证码返回的JSON
     *
     * @param JSON 验证码返回的JSON
     * @return 验证码实体类
     * @throws JSONException
     */
    public static Code parseCode(String JSON) throws JSONException {
        Code code = null;
        String c_JSON = null;
        if (JSON != null) {
            c_JSON = JSON;
            JSONObject jsonObject = new JSONObject(c_JSON);
            String status = jsonObject.getString("status");
            String tips = jsonObject.getString("tips");
            if (STATE_SUCCESS.equals(status)) {
                String codeId = jsonObject.getString("codeId");
                code = new Code(status, codeId, tips);
            } else {
                code = new Code(status, tips);
            }
        }

        return code;

    }

    /**
     * 解析返回的注册信息
     *
     * @param JSON 注册信息的JSON字符串
     * @return 注册信息类
     * @throws JSONException
     */

    public static RegInfo parseReg(String JSON) throws JSONException {
        RegInfo info = null;
        String c_JSON = null;
        if (JSON != null) {
            c_JSON = JSON;
            JSONObject jsonObject = new JSONObject(c_JSON);
            String status = jsonObject.getString("status");
            if (STATE_FAILURE.equals(status)) {
                String tips = jsonObject.getString("tips");
                info = new RegInfo(status, tips);
            } else {
                String uid = jsonObject.getString("uid");
                int c_uid = Integer.valueOf(uid);
                info = new RegInfo(status, c_uid);
            }
        }

        return info;

    }

    /**
     * 解析首页返回的JSON并存入数据库
     *
     * @param JSON 首页返回的JSON
     * @throws JSONException
     */

    public static void parseIndex(String JSON) throws JSONException {
        List<Banner> banners = new ArrayList<>();
        List<Information> informations = new ArrayList<>();
        List<ProjectInfo> projectInfos = new ArrayList<>();
        String c_JSON = null;
        if (JSON != null) {
            c_JSON = JSON;
            JSONObject jsonObject = new JSONObject(c_JSON);
            //首页Banner的JSONArray
            JSONArray jsonBanner = jsonObject.getJSONArray("banner");
            //首页资讯的JSONArray
            JSONArray jsonInformation = jsonObject.getJSONArray("zuixin_list");
            //首页展示项目的JSONArray
            JSONArray jsonProject = jsonObject.getJSONArray("borrow_list_rec");
            int b_length = jsonBanner.length();
            int i_length = jsonInformation.length();
            int j_length = jsonProject.length();
            for (int i = 0; i < b_length; i++) {
                jsonObject = jsonBanner.getJSONObject(i);
                String img = jsonObject.getString("img");
                String url = jsonObject.getString("url");
                Banner banner = new Banner(img, url);
                banners.add(banner);
            }
            for (int i = 0; i < i_length; i++) {
                jsonObject = jsonInformation.getJSONObject(i);
                String id = jsonObject.getString("id");
                String title = jsonObject.getString("title");
                Information information = new Information(id, title);
                informations.add(information);
            }

            for (int i = 0; i < j_length; i++){
                jsonObject = jsonProject.getJSONObject(i);
                String id = jsonObject.getString("id");
                String borrow_type = jsonObject.getString("borrow_type");
                String borrow_name = jsonObject.getString("borrow_name");
                String borrow_money = jsonObject.getString("borrow_money");
                String has_borrow = jsonObject.getString("has_borrow");
                String borrow_times = jsonObject.getString("borrow_times");
                String borrow_img = jsonObject.getString("borrow_img");
                String borrow_duration = jsonObject.getString("borrow_duration");
                String borrow_interest_rate = jsonObject.getString("borrow_interest_rate");
                String progress = jsonObject.getString("progress");
                String borrow_info = jsonObject.getString("borrow_info");
                String borrow_status_cn = jsonObject.getString("borrow_status_cn");
                ProjectInfo info = new ProjectInfo(id,borrow_type,borrow_name,borrow_money,has_borrow,borrow_times,borrow_img,borrow_duration,borrow_interest_rate,progress,borrow_info,borrow_status_cn);
                projectInfos.add(info);
            }

            Log.d(TAG, "banner：" + banners.toString() + ",information：" + informations.toString());
            //将首页banner添加到数据库
            BaseApplication.dbIndexController.addBanners(banners);
            //将首页资讯添加到数据库
            BaseApplication.dbIndexController.addInformation(informations);
            //将首页展示项目添加到数据库
            BaseApplication.dbIndexController.addProinfo(projectInfos);
        }
    }


}
